
import React from 'react'
class App extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            count: 10000
        }
    }
    addCount异步 = () => {
        //1.setState是否异步执行?是,在合成事件里面是一步的
        //2.在生命周期和定时器里面是同步的
        this.setState({
            //不要直接修改state
            count: this.state.count + 1
        })
        console.log(this.state.count)
    }
    addCount = () => {
        //把异步的setState变成同步的修改
        this.setState((state) => {
            return {
                count: state.count + 1
            }
        }, () => {
            //在第二个回调函数里获取最新的state
            console.log(this.state.count)
        })

    }
    render() {
        const { count } = this.state
        
        return <>
            <h1>{count}</h1>
            <button onClick={this.addCount}>累加</button>
        </>
    }
}

export default App